﻿package {
	import flash.display.*;
	import flash.net.*;
	import flash.text.*;
	import flash.events.*;
	public class A extends Sprite {
		private var output:TextField;
		private var receivingLC:LocalConnection;
		private var sendingLC:LocalConnection;
		public function A() {
			buildUI();
			receivingLC = new LocalConnection();
			receivingLC.allowDomain("*");
			receivingLC.client = this;
			receivingLC.addEventListener(StatusEvent.STATUS, statusHandler);
			receivingLC.addEventListener(AsyncErrorEvent.ASYNC_ERROR, errorHandler);
			receivingLC.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
			try {
				receivingLC.connect("_aConnection");
			} catch (error:ArgumentError) {
				trace("Can't connect...the connection name is already being used by another SWF");
			}
		}
		private function statusHandler(e:StatusEvent):void {
			switch (e.level) {
				case "status" :
					trace("LocalConnection.send() succeeded");
					break;
				case "error" :
					output.appendText("LocalConnection.send() failed\n");
					break;
				default:
			}
		}
		private function errorHandler(e:Event):void{
			output.appendText("SecurityError!\n");
		}
		public function outputHandler(msg:String):void {
			output.appendText(msg + "\n");
		}
		private function buildUI():void {
			output = new TextField();
			output.type = TextFieldType.INPUT;
			output.background = true;
			output.border = true;
			output.wordWrap = true;
			output.width = 300;
			output.x = (stage.stageWidth - output.width)/2;
			output.y = (stage.stageHeight - output.height)/2;
			output.text = "This is a.swf, Click me to get message from b.swf:\n";
			output.addEventListener(MouseEvent.CLICK, getHandler);
			addChild(output);
		}
		private function getHandler(e:Event):void {
			sendingLC = new LocalConnection();
			sendingLC.allowDomain("*");
			sendingLC.addEventListener(StatusEvent.STATUS, statusHandler);
			sendingLC.addEventListener(AsyncErrorEvent.ASYNC_ERROR, errorHandler);
			sendingLC.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
			sendingLC.send("_bConnection", "sendHandler", "send");
		}
	}
}